*******************************************************************
* This file generates descriptive statistics on hierarchies 
* and within-firm inequality
* 
* It estimates results for Table 1-3, Figure 1, Appendix B
*******************************************************************

clear all
set more off

cd "${data_dir}"
use workerdata_layer.dta, clear

* private firms
keep if sektor == "2"
keep if aar >= 1999 & aar <= 2008 

set more off
destring aar, replace

merge n:1 aar cvrnr using hierarchy_IV_data.dta, ///
keepusing(sample layers)
drop if _merge == 2
drop _merge

merge 1:1 aar pnr using wage_residuals.dta
drop if _merge == 2
drop _merge

keep aar pnr cvrnr layer timelon joblon sample funk3n ///
wageres* wageprojection* layers

* CPI deflator
gen CPI = 0
* base year is 2000
replace CPI = 5104/5253 if aar == "1999"
replace CPI = 5253/5253 if aar == "2000"
replace CPI = 5377/5253 if aar == "2001"
replace CPI = 5507/5253 if aar == "2002"
replace CPI = 5622/5253 if aar == "2003"
replace CPI = 5687/5253 if aar == "2004"
replace CPI = 5790/5253 if aar == "2005"
replace CPI = 5900/5253 if aar == "2006"
replace CPI = 6001/5253 if aar == "2007"
replace CPI = 6205/5253 if aar == "2008"

replace timelon = timelon/CPI
replace joblon = joblon/CPI
drop if cvrnr == ""
drop if timelon == . | timelon == 0
drop if joblon == . | joblon == 0
gen hours = joblon/timelon

* Drop outliers in hourly wages by year: lowest and highest 0.25%
bys aar: egen p025wage = pctile(timelon), p(0.25)
bys aar: egen p9975wage = pctile(timelon), p(99.75)
drop if timelon < p025wage | timelon > p9975wage
replace timelon = . if timelon == 0

* use log wages
replace timelon = log(timelon)
drop if timelon == .

* layers data for firms (Caliendo et al)
by aar cvrnr, sort: egen minlayer = min(layer)
gen layerN = 1 if layer == minlayer
foreach lay of numlist 2/4 {
   gen layerright = layer if layer>minlayer & minlayer!=.
   drop minlayer
   by aar cvrnr: egen minlayer = min(layerright) 
   replace layerN = `lay' if layer == minlayer
   drop layerright
}
drop minlayer
replace layerN = . if layers == .


* (1) Within and Across ALL Firms:
***************************************
set more off
sort aar cvrnr layerN funk3n
gen observed = layerN != .
tab aar observed
tab aar if layers == .


* Layers AND occupations:
set more off
preserve
keep if layers != .
sort aar cvrnr layerN funk3n
* hours-weighted log wages
by aar: egen meanwage = mean(timelon)
by aar cvrnr: egen meanwagefirm = mean(timelon)
by aar cvrnr layerN: egen meanwagefirmlayer = mean(timelon)
by aar cvrnr layerN funk3n: egen meanwagefirmlayerocc = mean(timelon)

* the weight is determined based on total hours each year
by aar: egen totalhours = total(hours)
gen weight = hours / totalhours
sum weight, det
gen withinlayer = weight * ((timelon - meanwagefirmlayerocc)^2)
sum withinlayer, det
gen withinlayeracrossocc = weight * ((meanwagefirmlayerocc - meanwagefirmlayer)^2)
sum withinlayeracrossocc, det
gen within = weight * ((meanwagefirmlayer - meanwagefirm)^2)
sum within, det
gen across = weight * ((meanwagefirm - meanwage)^2)
sum across, det

collapse (sum) withinlayer withinlayeracrossocc within across, by(aar)
gen total = withinlayer +  withinlayeracrossocc + within + across

gen sharewithin = 100*withinlayer/total
gen sharelayerocc = 100*withinlayeracrossocc/total
gen sharelayer = 100*within/total
gen shareacross = 100*across/total

label var sharewithin "Within-Firms, Within-Layers, Within Occ"
label var sharelayerocc "Within-Firms, Within-Layers, Across Occ"
label var sharelayer "Within-Firms, Across-Layers"
label var shareacross "Across-Firms"


tabout aar using "${results_dir}\Table1_annual.txt", sum c(mean withinlayer mean withinlayeracrossocc mean within mean across mean total) ///
f(3c) h2(_|All Firms Hours-Weighted) replace

collapse within withinlayer withinlayeracrossocc across
gen total = withinlayer +  withinlayeracrossocc + within + across

gen sharewithin = 100*withinlayer/total
gen sharelayerocc = 100*withinlayeracrossocc/total
gen sharelayer = 100*within/total
gen shareacross = 100*across/total

label var sharewithin "Within-Firms, Within-Layers, Within Occ"
label var sharelayerocc "Within-Firms, Within-Layers, Across Occ"
label var sharelayer "Within-Firms, Across-Layers"
label var shareacross "Across-Firms"
label var total "Total Wage Variance"

gen eval = 1

tabout eval using "${results_dir}\Table1.txt", sum c(mean sharewithin mean sharelayerocc mean sharelayer mean shareacross mean total) ///
f(3c) h2(_|All Firms Hours-Weighted) append
restore


* (2) Main sample

preserve
keep if sample == 1
keep if layers != .
sort aar cvrnr layerN funk3n
* hours-weighted log wages
by aar: egen meanwage = mean(timelon)
by aar cvrnr: egen meanwagefirm = mean(timelon)
by aar cvrnr layerN: egen meanwagefirmlayer = mean(timelon)
by aar cvrnr layerN funk3n: egen meanwagefirmlayerocc = mean(timelon)

* the weight is determined based on total hours each year
by aar: egen totalhours = total(hours)
gen weight = hours / totalhours
sum weight, det
gen withinlayer = weight * ((timelon - meanwagefirmlayerocc)^2)
sum withinlayer, det
gen withinlayeracrossocc = weight * ((meanwagefirmlayerocc - meanwagefirmlayer)^2)
sum withinlayeracrossocc, det
gen within = weight * ((meanwagefirmlayer - meanwagefirm)^2)
sum within, det
gen across = weight * ((meanwagefirm - meanwage)^2)
sum across, det

collapse (sum) withinlayer withinlayeracrossocc within across, by(aar)
gen total = withinlayer +  withinlayeracrossocc + within + across

gen sharewithin = 100*withinlayer/total
gen sharelayerocc = 100*withinlayeracrossocc/total
gen sharelayer = 100*within/total
gen shareacross = 100*across/total

label var sharewithin "Within-Firms, Within-Layers, Within Occ"
label var sharelayerocc "Within-Firms, Within-Layers, Across Occ"
label var sharelayer "Within-Firms, Across-Layers"
label var shareacross "Across-Firms"


tabout aar using "${results_dir}\Table1_annual.txt", sum c(mean withinlayer mean withinlayeracrossocc mean within mean across mean total) ///
f(3c) h2(_|All Firms Hours-Weighted) append

collapse within withinlayer withinlayeracrossocc across
gen total = withinlayer +  withinlayeracrossocc + within + across

gen sharewithin = 100*withinlayer/total
gen sharelayerocc = 100*withinlayeracrossocc/total
gen sharelayer = 100*within/total
gen shareacross = 100*across/total

label var sharewithin "Within-Firms, Within-Layers, Within Occ"
label var sharelayerocc "Within-Firms, Within-Layers, Across Occ"
label var sharelayer "Within-Firms, Across-Layers"
label var shareacross "Across-Firms"
label var total "Total Wage Variance"

gen eval = 1

tabout eval using "${results_dir}\Table1.txt", sum c(mean sharewithin mean sharelayerocc mean sharelayer mean shareacross mean total) ///
f(3c) h2(_|Sample Hours-Weighted) append
restore



*****************************
* RESIDUAL WAGES
*****************************

sort aar cvrnr layerN funk3n


* (1) All Firms:
*****************************
set more off
preserve
keep if layers != .

* compute yearly variance shares:
local x = 2
by aar: egen varobs`x' = sd(wageprojection`x')
replace varobs`x' = varobs`x'^2
by aar: egen varres`x' = sd(wageres`x')
replace varres`x' = varres`x'^2


* Hours-weighted measure:
****************************
by aar: egen totalhours = total(hours)
gen weight = hours / totalhours

by aar: egen meanwageres`x' = mean(wageres`x')
by aar cvrnr: egen firmmeanwageres`x' = mean(wageres`x')
by aar cvrnr layerN: egen firmlayermeanwageres`x' = mean(wageres`x')
by aar cvrnr layerN funk3n: egen meanwagefirmlayerocc`x' = mean(wageres`x')

gen resshare`x' = varres`x' / (varobs`x' + varres`x')
gen withinlayerocc`x' = weight * ((wageres`x' - meanwagefirmlayerocc`x')^2)
gen withinlayer`x' = weight * ((meanwagefirmlayerocc`x' - firmlayermeanwageres`x')^2)
gen within`x' = weight * ((firmlayermeanwageres`x' - firmmeanwageres`x')^2)
gen across`x' = weight * ((firmmeanwageres`x' - meanwageres`x')^2)


collapse (sum) withinlayerocc`x' withinlayer`x' within`x' across`x' varres`x' (mean) resshare`x', by(aar)

gen total`x' = withinlayerocc`x' + withinlayer`x' + within`x' + across`x'
gen sharewithinocc`x' = 100*withinlayerocc`x'/total`x'
gen sharewithin`x' = 100*withinlayer`x'/total`x'
gen sharelayer`x' = 100*within`x'/total`x'
gen shareacross`x' = 100*across`x'/total`x'

tabout aar using "${results_dir}\Table1_annual.txt", sum c(mean withinlayerocc`x' mean withinlayer`x' mean within`x' mean across`x' mean total`x') ///
f(3c) h2(_|All Firms Hours-Weighted Model `x') append


collapse withinlayerocc`x' withinlayer`x' within`x' across`x' varres`x' resshare`x'

gen total`x' = withinlayerocc`x' + withinlayer`x' + within`x' + across`x'
gen sharewithinocc`x' = 100*withinlayerocc`x'/total`x'
gen sharewithin`x' = 100*withinlayer`x'/total`x'
gen sharelayer`x' = 100*within`x'/total`x'
gen shareacross`x' = 100*across`x'/total`x'


label var sharewithinocc`x' "Within-Firms, Within-Layer, Within Occ"
label var sharewithin`x' "Within-Firms, Within-Layer, Across Occ"
label var sharelayer`x' "Within-Firms, Across-Layer"
label var shareacross`x' "Across-Firms"

gen eval = 1
tabout eval using "${results_dir}\Table1.txt", sum c(mean sharewithinocc`x' mean sharewithin`x' mean sharelayer`x' mean shareacross`x' mean total`x') ///
f(3c) h2(_|All Firms Hours-weighted) append
restore


* (2) Main Sample:
set more off

preserve
keep if sample == 1
sort aar cvrnr layerN funk3n
keep if layers != .

* compute yearly variance shares:
local x = 2
by aar: egen varobs`x' = sd(wageprojection`x')
replace varobs`x' = varobs`x'^2
by aar: egen varres`x' = sd(wageres`x')
replace varres`x' = varres`x'^2

* Hours-weighted measure:
****************************
by aar: egen totalhours = total(hours)
gen weight = hours / totalhours

by aar: egen meanwageres`x' = mean(wageres`x')
by aar cvrnr: egen firmmeanwageres`x' = mean(wageres`x')
by aar cvrnr layerN: egen firmlayermeanwageres`x' = mean(wageres`x')
by aar cvrnr layerN funk3n: egen meanwagefirmlayerocc`x' = mean(wageres`x')

gen resshare`x' = varres`x' / (varobs`x' + varres`x')
gen withinlayerocc`x' = weight * ((wageres`x' - meanwagefirmlayerocc`x')^2)
gen withinlayer`x' = weight * ((meanwagefirmlayerocc`x' - firmlayermeanwageres`x')^2)
gen within`x' = weight * ((firmlayermeanwageres`x' - firmmeanwageres`x')^2)
gen across`x' = weight * ((firmmeanwageres`x' - meanwageres`x')^2)


collapse (sum) withinlayerocc`x' withinlayer`x' within`x' across`x' varres`x' (mean) resshare`x', by(aar)

gen total`x' = withinlayerocc`x' + withinlayer`x' + within`x' + across`x'
gen sharewithinocc`x' = 100*withinlayerocc`x'/total`x'
gen sharewithin`x' = 100*withinlayer`x'/total`x'
gen sharelayer`x' = 100*within`x'/total`x'
gen shareacross`x' = 100*across`x'/total`x'

tabout aar using "${results_dir}\Table1_annual.txt", sum c(mean withinlayerocc`x' mean withinlayer`x' mean within`x' mean across`x' mean total`x') ///
f(3c) h2(_|Main Sample Hours-Weighted Model `x') append


collapse withinlayerocc`x' withinlayer`x' within`x' across`x' varres`x' resshare`x'

gen total`x' = withinlayerocc`x' + withinlayer`x' + within`x' + across`x'
gen sharewithinocc`x' = 100*withinlayerocc`x'/total`x'
gen sharewithin`x' = 100*withinlayer`x'/total`x'
gen sharelayer`x' = 100*within`x'/total`x'
gen shareacross`x' = 100*across`x'/total`x'


label var sharewithinocc`x' "Within-Firms, Within-Layer, Within Occ"
label var sharewithin`x' "Within-Firms, Within-Layer, Across Occ"
label var sharelayer`x' "Within-Firms, Across-Layer"
label var shareacross`x' "Across-Firms"

gen eval = 1
tabout eval using "${results_dir}\Table1.txt", sum c(mean sharewithinocc`x' mean sharewithin`x' mean sharelayer`x' mean shareacross`x' mean total`x') ///
f(3c) h2(_|Main Sample Hours-Weighted Model `x') append
restore


********************************
* Hierarchy Descriptives
********************************
cd "${data_dir}"
use hierarchy_IV_data, clear

********************
* TABLE 2 Panel A
********************
label var totalnw "Average Wage per Layer"
label var totalh "Total Hours"
gen eval = 1
gen relsales = sales/(8.09*1000000)
gen relexp = trade2/(8.09*1000000)
gen relwage = totalnw/8.09
gen relhours = totalh/1000

cd "${results_dir}"
tabout layers if sample == 1 using Table2_PanelA.txt, format(2) sum ///
c(sum eval mean relsales mean wagegap1_topbottom_log ///
mean loginequality4) replace

* Table A
tabout layers if sample == 1 using TableA6.txt, format(1) sum ///
c(sum eval mean relhours mean relsales mean relexp mean relwage) replace

tabout layers using TableA6.txt, format(1) sum ///
c(sum eval mean relhours mean relsales mean relexp mean relwage) append


********************
* TABLE 2 Panel B
********************
destring aar, replace
preserve
keep if sample == 1
tab aar
keep aar cvrnr layers
reshape wide layers, i(cvrnr) j(aar)

matrix define Mobserved = J(4,1,0)
matrix define Mtransitions = J(4,5,0)

foreach year of numlist 1999/2007 {

  foreach nlay1 of numlist 1/4 {
   quietly su if layers`year'==`nlay1'
   matrix Mobserved[`nlay1',1] = Mobserved[`nlay1',1] + r(N)
   
   local nextyear = `year'+1
   foreach nlay2 of numlist 1/4 {
	   quietly su if layers`year'==`nlay1' & layers`nextyear'==`nlay2'
       matrix Mtransitions[`nlay1',`nlay2'] = Mtransitions[`nlay1',`nlay2'] + r(N)
	}
	quietly su if layers`year'==`nlay1' & layers`nextyear'==.
    matrix Mtransitions[`nlay1',5] = Mtransitions[`nlay1',5] + r(N) 
   }
}

  foreach nlay1 of numlist 1/4 {
     foreach nlay2 of numlist 1/5 {
        matrix Mtransitions[`nlay1',`nlay2'] = Mtransitions[`nlay1',`nlay2']/Mobserved[`nlay1',1]
	}
  }
  
matrix list Mtransitions
svmat Mtransitions
gen ind = _n
tabout ind if ind <5 using Table2_PanelB.txt, sum f(3c) c(mean Mtransitions1 mean Mtransitions2 mean Mtransitions3 mean Mtransitions4 mean Mtransitions5) replace
restore

* TABLE 2 Panel C
********************
* Share of firms with x layers that have occupational layer 1-4: 
log using Table2_PanelC.txt, text replace

table layers if sample==1, c(mean occlayerind1 mean occlayerind2 ///
mean occlayerind3 mean occlayerind4)

log close

* TABLE A7 Panel C
********************
sort cvrnr aar
* Type of layer added of dropped:
forvalues x = 1/4{
by cvrnr: gen drop`x' = occlayerind`x' - occlayerind`x'[_n-1] == -1 ///
if occlayerind`x' != . & occlayerind`x'[_n-1] != .
by cvrnr: gen add`x' = occlayerind`x' - occlayerind`x'[_n-1] == 1 ///
if occlayerind`x' != . & occlayerind`x'[_n-1] != .
}
cap drop prevlayers
by cvrnr: gen prevlayers = layers[_n-1]

tabout prevlayers if sample == 1 & adddrop == 1 ///
using TableA7_PanelC.txt, sum f(3c) ///
c(mean add1 mean add2 mean add3 mean add4) replace

tabout prevlayers if sample == 1 & adddrop == -1 ///
using TableA7_PanelC.txt, sum f(3c) ///
c(mean drop1 mean drop2 mean drop3 mean drop4) append


* TABLE 2 Panel D
********************
sort cvrnr aar
gen sumocc = occlayerind1 +occlayerind2 +occlayerind3 +occlayerind4

* Determine if added/dropped layer is the highest:
cap drop maxocclayer
gen maxocclayer = 1 if (sumocc==1 & occlayerind1==1)
replace maxocclayer = 2 if (sumocc==1 & occlayerind2==1) ///
| (sumocc==2 & occlayerind1==1 & occlayerind2==1)
replace maxocclayer = 3 if (sumocc==1 & occlayerind3==1) ///
| (sumocc==2 & occlayerind1==1 & occlayerind3==1) ///
| (sumocc==3 & occlayerind1==1 & occlayerind2==1 & occlayerind3==1)
replace maxocclayer = 4 if (sumocc==1 & occlayerind4==1) ///
| (sumocc==2 & occlayerind1==1 & occlayerind4==1) ///
| (sumocc==3 & occlayerind1==1 & occlayerind4==1) | sumocc == 4

by cvrnr: gen drophighest = maxocclayer < maxocclayer[_n-1] if maxocclayer != . & maxocclayer[_n-1] != .
by cvrnr: gen addhighest =  maxocclayer > maxocclayer[_n-1] if maxocclayer != . & maxocclayer[_n-1] != .

preserve
keep if sample == 1
xtset firmcode aar

log using Table2_PanelD.txt, text replace

table prevlayers layers if adddrop == 1, ///
c(mean addhighest)
table prevlayers layers if adddrop == -1, ///
c(mean drophighest)

log close

restore

* TABLE A7 Panel D-F
***********************
* How do firms add/drop layers?
cd "${data_dir}"
use aar pnr cvrnr timelon using workerdata_full.dta, clear

* add occupational layer information by worker
merge 1:1 aar pnr using workerdata_layer.dta, keepusing(layer)
drop _merge

set more off
sort pnr aar
destring aar, replace
* previous period variables:
by pnr: gen prevtimelon = timelon[_n-1] if aar == aar[_n-1]+1
by pnr: gen prevlayer = layer[_n-1] if aar == aar[_n-1]+1
by pnr: gen prevcvrnr = cvrnr[_n-1] if aar == aar[_n-1]+1
* next period variables:
by pnr: gen nexttimelon = timelon[_n+1] if aar == aar[_n+1]-1
by pnr: gen nextlayer = layer[_n+1] if aar == aar[_n+1]-1
by pnr: gen nextcvrnr = cvrnr[_n+1] if aar == aar[_n+1]-1

merge n:1 aar cvrnr using hierarchy_IV_data, keepusing(layers sample adddrop nextadddrop occlayerind*)

gen new = prevcvrnr != cvrnr & cvrnr != ""
gen exit = nextcvrnr != cvrnr & cvrnr != ""
gen demoted = prevlayer > layer & layer != . & prevlayer != .
gen promoted = prevlayer < layer & layer != . & prevlayer != .
gen nextdemoted = layer > nextlayer & layer != . & nextlayer != .
gen nextpromoted = layer < nextlayer & layer != . & nextlayer != .

sort pnr aar
by pnr: gen prevexit = exit[_n-1] if aar == aar[_n-1]+1
by pnr: gen nextdrop = nextadddrop==-1
by pnr: gen prevnextdrop = nextdrop[_n-1] if aar == aar[_n-1]+1
by pnr: gen nextadd = nextadddrop==1
by pnr: gen prevnextadd = nextadd[_n-1] if aar == aar[_n-1]+1
gen dwage = log(timelon) - log(prevtimelon)
gen dlayer = layer - prevlayer

cd "${results_dir}"

* Panel D: Composition of Hiring/Firing
sort cvrnr aar
cap drop eval
by cvrnr aar: gen eval = _n==1
forvalues x = 1/4{
gen obs`x' = 0 if layer == `x' 
replace obs`x' = 1 if layer == `x'

gen new`x' = 0 if layer == `x' 
replace new`x' = 1 if layer == `x' & new == 1

gen exit`x' = 0 if layer == `x' 
replace exit`x' = 1 if layer == `x' & exit == 1
}

preserve
keep if sample == 1

forvalues x = 1/4{
    
* all workers by layer
by cvrnr aar: egen ntotal`x' = total(obs`x')

* share of new workers by layer (for adding layers):
by cvrnr aar: egen nnew`x' = total(new`x')
gen sharenew`x' = nnew`x' / ntotal`x'

* share of workers leaving by layer (for dropping layers):
by cvrnr aar: egen nexit`x' = total(exit`x')
gen shareexit`x' = nexit`x' / ntotal`x'
}

keep if eval == 1

sort cvrnr aar
forvalues x = 1/4{
by cvrnr: gen prevshareexit`x' = shareexit`x'[_n-1] if aar == aar[_n-1]+1
}
forvalues x = 1/4{
by cvrnr: gen drop`x' = occlayerind`x' - occlayerind`x'[_n-1] == -1 if occlayerind`x' != . & occlayerind`x'[_n-1] != .
by cvrnr: gen add`x' = occlayerind`x' - occlayerind`x'[_n-1] == 1 if occlayerind`x' != . & occlayerind`x'[_n-1] != .
}

gen totaladd = add1 + add2 + add3 + add4
gen totaldrop = drop1 + drop2 + drop3 + drop4

gen droppedlayer = 1 if drop1 == 1
replace droppedlayer = 2 if drop2 == 1
replace droppedlayer = 3 if drop3 == 1
replace droppedlayer = 4 if drop4 == 1
gen shareexitrel = prevshareexit1 if drop1 == 1
replace shareexitrel = prevshareexit2 if drop2 == 1
replace shareexitrel = prevshareexit3 if drop3 == 1
replace shareexitrel = prevshareexit4 if drop4 == 1

tabout droppedlayer if sample == 1 & adddrop == -1 ///
& totaldrop == 1 using TableA7_PanelD.txt, sum f(3c) ///
c(mean shareexitrel) replace

gen addedlayer = 1 if add1 == 1
replace addedlayer = 2 if add2 == 1
replace addedlayer = 3 if add3 == 1
replace addedlayer = 4 if add4 == 1
gen sharenewrel = sharenew1 if add1 == 1
replace sharenewrel = sharenew2 if add2 == 1
replace sharenewrel = sharenew3 if add3 == 1
replace sharenewrel = sharenew4 if add4 == 1

tabout addedlayer if sample == 1 & adddrop == 1 ///
& totaladd == 1 using TableA7_PanelD.txt, sum f(3c) ///
c(mean sharenewrel) append

* For stable firms:
tabout sample if sample == 1 & adddrop == 0 & totaladd == 0 & totaldrop == 0 ///
using TableA7_PanelE.txt, sum f(3c) ///
c(mean prevshareexit1 mean prevshareexit2 mean prevshareexit3 mean prevshareexit4) append

tabout sample if sample == 1 & adddrop == 0 & totaladd == 0 & totaldrop == 0 ///
using TableA7_PanelE.txt, sum f(3c) ///
c(mean sharenew1 mean sharenew2 mean sharenew3 mean sharenew4) append

restore

* Panel F: Wage Changes during Restructuring

* Wage changes for workers at firms that add/drop layers,
* for workers who are promoted / demoted or hired from outside

* is the worker at a firm in the sample?
sort pnr aar
by pnr: gen prevsample = sample[_n-1] if aar == aar[_n-1]+1

preserve

* generate a dummy variable for the status of the worker (stayer, layer promotion):
cap drop status
* stayer, same layer
gen status = 1 if prevcvrnr == cvrnr & prevlayer == layer & prevlayer != . & layer != .
* stayer, promotion
replace status = 2 if prevcvrnr == cvrnr & prevlayer < layer & prevlayer != . & layer != .
* stayer, demotion
replace status = 3 if prevcvrnr == cvrnr & prevlayer > layer & prevlayer != . & layer != .

* new, same layer
replace status = 4 if new == 1 & layer == prevlayer & prevlayer != . & layer != .
* new, enter promotion
replace status = 5 if new == 1 & prevlayer < layer & prevlayer != . & layer != .
* new, enter demotion
replace status = 6 if new == 1 & prevlayer > layer & prevlayer != . & layer != .

log using TableA7_PanelE.txt, text replace
* wage change for workers if the firm adds a layer:
tabstat dwage if adddrop == 1 & sample == 1, stats(N mean) by(status)
log close

* prev exit, same layer
replace status = 7 if prevexit == 1 & layer == prevlayer & prevlayer != . & layer != .
* prev exit, enter promotion
replace status = 8 if prevexit == 1 & prevlayer < layer & prevlayer != . & layer != .
* prev exit, enter demotion
replace status = 9 if prevexit == 1 & prevlayer > layer & prevlayer != . & layer != .
* prevexit, no job omitted

log using TableA7_PanelE.txt, text append
* wage change for workers if the firm drops a layer:
tabstat dwage if prevnextdrop == 1 & prevsample == 1, stats(N mean) by(status)
log close

restore

cd "${data_dir}"
use hierarchy_IV_data.dta, replace

cd "${results_dir}"

********************
* Figure 1 
********************
preserve
keep if sample == 1
* plot sales distribution
gen logn = log(sales)
forvalues l = 1/4{
	kdensity logn if layers == `l', n(400) generate(lnx`l' lny`l') nograph
	replace lnx`l' = exp(lnx`l')/1000000
}
sum sales, detail
local minx = 0.1
local maxx = r(max)
line lny1 lnx1 if lnx1 >= `minx' & lnx1 <= `maxx', lcolor(black) lpattern(shortdash) xscale(log) || /*
*/	line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) xscale(log) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) xscale(log) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) xscale(log)  /*
*/	legend(label(1 "1 layer") label(2 "2 layers") label(3 "3 layers") label(4 "4 layers"))  /*
*/	xtitle("Sales [in Million DKK]") ytitle(Density)  /*
*/  note("Epanechnikov kernel density estimate, raw data") /*
*/	xlabel(1 10 100 1000 10000) graphregion(color(white))
graph export "Figure1_sales.pdf", replace
restore

preserve
keep if sample == 1
* plot average wage gap distribution
forvalues l = 1/4{
	kdensity wagegap1_topbottom_log if layers == `l', n(400) generate(lnx`l' lny`l') nograph
}
sum wagegap1_topbottom_log, detail
local minx = r(p1)
local maxx = 3
tabstat wagegap1_topbottom_log if wagegap1_topbottom_log >= `minx' & wagegap1_topbottom_log <= `maxx', stats(N mean) by(layers)
line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) /*
*/	legend(cols(3) label(1 "2 layers") label(2 "3 layers") label(3 "4 layers"))  /*
*/	xtitle("Wage Gap Between Top and Bottom Layer") ytitle(Density) note("Epanechnikov kernel density estimate, raw data") ///
	graphregion(color(white))
graph export "Figure1_wagegap_topbottom.pdf", replace
restore

preserve
keep if sample == 1

* plot average sd(wages)
forvalues l = 1/4{
	kdensity loginequality1 if layers == `l', n(400) generate(lnx`l' lny`l') nograph
	disp `r(bwidth)'
}
sum loginequality1, detail
local minx = r(p1)
local maxx = r(p99)
tabstat loginequality1 if loginequality1 >= `minx' & loginequality1 <= `maxx', stats(N mean) by(layers)
line lny1 lnx1 if lnx1 >= `minx' & lnx1 <= `maxx', lcolor(black) lpattern(shortdash) || /*
*/	line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) /*
*/	legend(label(1 "1 layer") label(2 "2 layers") label(3 "3 layers") label(4 "4 layers"))  /*
*/	xtitle("SD(Log Wages)") ytitle(Density) note("Epanechnikov kernel density estimate, raw data") ///
	graphregion(color(white))
graph export "FigureA2_sdwages.pdf", replace
restore

preserve
keep if sample == 1
* plot average 90-50 wage gap
forvalues l = 1/1{
	kdensity loginequality4 if layers == `l', n(400) bw(0.05) generate(lnx`l' lny`l') nograph
	disp `r(bwidth)'	
}

forvalues l = 2/4{
	kdensity loginequality4 if layers == `l', n(400) generate(lnx`l' lny`l') nograph
	disp `r(bwidth)'	
}

sum loginequality4, detail
local minx = r(p1)
local maxx = r(p99)
tabstat loginequality4 if loginequality4 >= `minx' & loginequality4 <= `maxx', stats(N mean) by(layers)
line lny1 lnx1 if lnx1 >= `minx' & lnx1 <= `maxx', lcolor(black) lpattern(shortdash) || /*
*/	line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) /*
*/	legend(label(1 "1 layer") label(2 "2 layers") label(3 "3 layers") label(4 "4 layers"))  /*
*/	xtitle("90-50 Wage Gap") ytitle(Density) note("Epanechnikov kernel density estimate, raw data") ///
	graphregion(color(white))
graph export "FigureA2_gap9050.pdf", replace
restore

preserve
keep if sample == 1
* plot average wage distribution
gen logn = log(totalnw)
forvalues l = 1/4{
	kdensity logn if layers == `l', n(400) generate(lnx`l' lny`l') nograph
	replace lnx`l' = exp(lnx`l')
}
sum totalnw, detail
local minx = 25
local maxx = r(p99)
line lny1 lnx1 if lnx1 >= `minx' & lnx1 <= `maxx', lcolor(black) lpattern(shortdash) || /*
*/	line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) /*
*/	legend(label(1 "1 layer") label(2 "2 layers") label(3 "3 layers") label(4 "4 layers"))  /*
*/	xtitle("Average Wages") ytitle(Density)   /*
*/  note("Epanechnikov kernel density estimate, raw data") ///
	graphregion(color(white))
graph export "FigureA2_wages.pdf", replace

* plot average hours distribution
drop logn lnx* lny*
gen logn = log(totalh)
forvalues l = 1/4{
	kdensity logn if layers == `l', n(400) generate(lnx`l' lny`l') nograph
	replace lnx`l' = exp(lnx`l')
}
sum totalh, detail
local minx = 0.1
local maxx = r(max)
line lny1 lnx1 if lnx1 >= `minx' & lnx1 <= `maxx', lcolor(black) lpattern(shortdash) || /*
*/	line lny2 lnx2 if lnx2 >= `minx' & lnx2 <= `maxx', lcolor(black) lpattern(longdash) || /*
*/	line lny3 lnx3 if lnx3 >= `minx' & lnx3 <= `maxx', lcolor(black) lpattern(dash_dot) || /*
*/	line lny4 lnx4 if lnx4 >= `minx' & lnx4 <= `maxx', lcolor(black) lpattern(solid) /*
*/	legend(label(1 "1 layer") label(2 "2 layers") label(3 "3 layers") label(4 "4 layers"))  /*
*/	xtitle("Total Hours") ytitle(Density) title("Hours Distribution by Number of Layers")  /*
*/  subtitle("Sample") note("Epanechnikov kernel density estimate, raw data") /*
*/	xlabel(1000 10000 100000 1000000) graphregion(color(white)) xscale(log)
graph export "FigureA2_hours.pdf", replace
restore

* Correlation Patterns Within Firms: 
**************************************

set more off

* Table 3 Panel A
*******************
areg wagegap1_topbottom_log layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) replace

areg wagegap1_topbottom_res2 layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg log2inequality1 layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg res2inequality1_model2 layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg log2inequality4 layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg res2inequality4_model2 layers i.t i.sector027 if samplecore3 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append


* Details: Table A8
*******************

set more off
foreach z in core3 {
foreach x in 1 3 {
areg wagegap`x'_topbottom_log layers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA8.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

foreach y in 2 4 {
areg wagegap`x'_topbottom_res`y' layers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA8.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append
}
}

foreach x in 1 2 4 5 {

areg log2inequality`x' layers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA8.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

foreach y in 2 4 {
areg res2inequality`x'_model`y' layers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA8.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

}
}
}


* Table 3 Panel B
*******************
gen stayersample = samplecore4==1 & dgap1_stay != . & dlog2preinequality4 != . ///
& dlog2preinequality1 != . & dgap3_stay != . & dlog2preinequality5 !=.

set more off

areg dgap1_log addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) replace 

areg dgap1_stay addlayers droplayers i.t i.sector027 if stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

foreach x in 1 4 {
areg dlog2inequality`x' addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' addlayers droplayers i.t i.sector027 if stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using Table3_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}


* Details: Table A9
*********************
set more off

foreach x in 1 3 {
areg dgap`x'_log addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap`x'_stay addlayers droplayers i.t i.sector027 if samplecore4 == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap`x'_res2 addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap`x'_res4 addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}

foreach x in 1 2 4 5 {
areg dlog2inequality`x' addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' addlayers droplayers i.t i.sector027 if samplecore4 == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model4 addlayers droplayers i.t i.sector027 if samplecore4 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA9.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}





****************************************************
* Robustness: Table 3
****************************************************

* Table A11: Industry-year FE
set more off
foreach z in core3 {

areg wagegap1_topbottom_log layers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA11_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg wagegap1_topbottom_res2 layers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA11_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append


foreach x in 1 4 {

areg log2inequality`x' layers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA11_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg res2inequality`x'_model2 layers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA11_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

}
}

* Table A10: Industry-year FE plus firm controls and skill composition
foreach z in core3 {

areg wagegap1_topbottom_log layers i.t#i.sector027  ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool ///
if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA10.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(layers ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool) adds("Firms", e(df_a)+1) append

areg wagegap1_topbottom_res2 layers i.t#i.sector027 ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool ///
if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA10.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(layers ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool) adds("Firms", e(df_a)+1) append

foreach x in 1 4 {

areg log2inequality`x' layers i.t#i.sector027 ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool ///
if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA10.tex, tex alpha(0.01, 0.05, 0.1)  ///
symbol(***,**,*) bdec(4) keep(layers ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool) adds("Firms", e(df_a)+1) append

areg res2inequality`x'_model2 layers i.t#i.sector027 ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool ///
if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA10.tex, tex alpha(0.01, 0.05, 0.1)  ///
symbol(***,**,*) bdec(4) keep(layers ///
logsales logempl firmage collegeshare avexperience sdexperience avschool sdschool) adds("Firms", e(df_a)+1) append
}
}


* Table A12: Sales growth
foreach z in core4 {

areg dgap1_log dsales addlayers droplayers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_stay dsales addlayers droplayers i.t i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_res2 dsales addlayers droplayers i.t i.sector027 if sample`z' == 1 ///
& correctlayers == 1 & L.correctlayers == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 
 
foreach x in 1 4 {
areg dlog2inequality`x' dsales addlayers droplayers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' dsales addlayers droplayers i.t i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 dsales addlayers droplayers i.t i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_dsales.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales addlayers droplayers) adds("Firms", e(df_a)+1) append 

}
}

* Table A12: Industry-year FE
foreach z in core4 {
areg dgap1_log addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_stay addlayers droplayers i.t#i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_res2 addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

foreach x in 1 4 {
areg dlog2inequality`x' addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' addlayers droplayers i.t#i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_indyear.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}
}


* Table A12: Full controls
foreach z in core4 {

areg dgap1_log dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_stay dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_res2 dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

foreach x in 1 4 {
areg dlog2inequality`x' dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers i.t#i.sector027 if sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA12_fullcontrols.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(dsales dcollege dexperience dsdexperience dschooling dsdschooling ///
addlayers droplayers) adds("Firms", e(df_a)+1) append 

}
}


* Table A14: By firm age Young
foreach z in core4 {

areg dgap1_log addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append

areg dgap1_stay addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_res2 addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

foreach x in 1 4 {
areg dlog2inequality`x' addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage <= 10, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_young.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}
}

* Table A14: By firm age Old
foreach z in core4 {
areg dgap1_log addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append

areg dgap1_stay addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dgap1_res2 addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

foreach x in 1 4 {
areg dlog2inequality`x' addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dlog2preinequality`x' addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25 & stayersample == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

areg dres2inequality`x'_model2 addlayers droplayers i.t i.sector027 if sample`z' == 1 & firmage >=25, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA14_old.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(addlayers droplayers) adds("Firms", e(df_a)+1) append 

}
}




* Table A13 Results by subperiod
gen period = 1 if aar <= 2003
replace period = 2 if aar >= 2004

foreach time in 1 2 {
foreach z in core3 {

areg wagegap1_topbottom_log layers i.t i.sector027 if period==`time' & sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA13_period`time'.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg wagegap1_topbottom_res2 layers i.t i.sector027 if period==`time' & sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA13_period`time'.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

foreach x in 1 4 {

areg log2inequality`x' layers i.t i.sector027 if period==`time' & sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA13_period`time'.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

areg res2inequality`x'_model2 layers i.t i.sector027 if period==`time' & sample`z' == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using TableA13_period`time'.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layers) adds("Firms", e(df_a)+1) append

}
}
}

* Table A15: span of control
******************************
cap drop span
gen span = (totaltopcounter + totalbottomcounter) / totaltopcounter

cap drop logspan
gen logspan = log(span)

areg wagegap1_topbottom_log layers logspan i.t sectordummy* ///
if samplecore3 == 1 & layers >= 2, absorb(firmcode) vce(cluster sector027)

outreg2 using TableA15.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(layers logspan) ///
adds("Firms", e(df_a)+1) append

foreach x in 1 2 4 5{
areg log2inequality`x' layers logspan i.t sectordummy* ///
if samplecore3 == 1 & layers >= 2, absorb(firmcode) vce(cluster sector027)

outreg2 using TableA15.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(layers logspan) ///
adds("Firms", e(df_a)+1) append

}


* Figure A.3: visualization
**********************************************
areg I_WID i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027)
predict xhat, res

areg logsales i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027)
predict saleshat, res

areg layers i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027)
predict layershat, res

areg wagegap3_topbottom_log i.t sectordummy* if sample0 == 1, ///
absorb(firmcode) vce(cluster sector027)
predict yhat3, res

areg log2inequality1 i.t sectordummy* if sample0 == 1, ///
absorb(firmcode) vce(cluster sector027)
predict yhat1, res

areg log2inequality4 i.t sectordummy* if sample0 == 1, ///
absorb(firmcode) vce(cluster sector027)
predict yhat4, res

areg log2inequality5 i.t sectordummy* if sample0 == 1, ///
absorb(firmcode) vce(cluster sector027)
predict yhat5, res

preserve
keep if sample == 1 


sum xhat if sample == 1, det
local low = -1
local high = 1

sum saleshat if sample == 1, det
local slow = -1
local shigh = 1

cap drop grid ysmooth sesmooth
lpoly saleshat xhat if xhat >`low' & xhat < `high', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle(Log Sales) xtitle(Trade Shock) ///
title(Trade Shocks and Sales) ///
graphregion(color(white)) legend(off)

graph export FigureA3_lpoly_sales_tradeshock.pdf, replace

cap drop grid ysmooth sesmooth
lpoly layershat saleshat if saleshat >`slow' & saleshat < `shigh', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle(Layers) xtitle(Log Sales) ///
title(Sales and Layers) ///
graphregion(color(white)) legend(off)

graph export FigureA3_lpoly_layers_sales.pdf, replace

cap drop grid ysmooth sesmooth
lpoly yhat3 xhat if xhat >`low' & xhat < `high', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle("Top-Bottom Wage Gap") xtitle(Trade Shock) ///
title("Trade Shocks and Top-Bottom Wage Gap") ///
graphregion(color(white)) legend(off) 

graph export FigureA3_lpoly_topbottom_tradeshock.pdf, replace


cap drop grid ysmooth sesmooth
lpoly yhat1 xhat if xhat >`low' & xhat < `high', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle("SD(Log Wages)") xtitle(Trade Shock) ///
title("Trade Shocks and SD(Log Wages)") ///
graphregion(color(white)) legend(off)

graph export FigureA3_lpoly_sdlogw_tradeshock.pdf, replace


cap drop grid ysmooth sesmooth
lpoly yhat4 xhat if xhat >`low' & xhat < `high', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle(90-50 Wage Gap) xtitle(Trade Shock) ///
title(Trade Shocks and 90-50 Wage Gap) ///
graphregion(color(white)) legend(off)

graph export FigureA3_lpoly_9050gap_tradeshocks.pdf, replace


cap drop grid ysmooth sesmooth
lpoly yhat5 xhat if xhat >`low' & xhat < `high', nograph degree(0) ///
gen(grid ysmooth) se(sesmooth) bw(0.25)

cap drop up 
gen up = ysmooth + 1.96*sesmooth
cap drop down 
gen down = ysmooth - 1.96*sesmooth

line ysmooth grid, lpattern(solid) lcolor(black) ///
|| line up grid, lpattern(dash) lcolor(black) ///
|| line down grid, lpattern(dash) lcolor(black) ///
ytitle(50-10 Wage Gap) xtitle(Trade Shock) ///
title(Trade Shocks and 50-10 Wage Gap) ///
graphregion(color(white)) legend(off)

graph export FigureA3_lpoly_5010gap_tradeshocks.pdf, replace


restore


